package com.example.dormmanage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormmanage.dao.IAreaDao;
import com.example.dormmanage.dao.IBuildingDao;
import com.example.dormmanage.entity.Area;
import com.example.dormmanage.entity.Building;
import com.example.dormmanage.service.IBuildingService;
import com.example.dormmanage.view.BuildingDto;
import com.example.dormmanage.view.ResultView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BuildingService extends ServiceImpl<IBuildingDao, Building> implements IBuildingService {

    @Autowired
    private IBuildingDao buildingDao;

    @Autowired
    private IAreaDao areaDao;

    //查询区域和楼宇列表
    public ResultView<List<BuildingDto>> selectBuildingList() {
        return ResultView.success(buildingDao.selectBuildingList());
    }

    //查询区域和楼宇列表
    public ResultView<List<BuildingDto>> selectBuildingList1(String name) {
        //查询区域id
        LambdaQueryWrapper<Area> lqw = new LambdaQueryWrapper();
        lqw.like(name != null,Area::getAreaName,name);
        List<Area> areaList = areaDao.selectList(lqw);
        //根据区域id查询区域中的楼宇
        List<BuildingDto> buildingDtoList1 = new ArrayList<>();
        for (int i = 0; i < areaList.size(); i++) {
            BuildingDto buildingDto = new BuildingDto();
            buildingDto.setAreaName(areaList.get(i).getAreaName());
            LambdaQueryWrapper<Building> buildingLqw = new LambdaQueryWrapper<>();
            buildingLqw.eq(Building::getAreaId, areaList.get(i).getAreaId());
            buildingDto.setBuilding(this.list(buildingLqw));
            buildingDtoList1.add(buildingDto);
        }
        //整合数据
        return ResultView.success(buildingDtoList1);
    }

    //新增楼宇
    public ResultView<String> addBuilding(Building building) {
        return null;
    }

    //获取楼宇名称(xx-xxx形式)
    public String getBuildingName(Integer buildingId) {
        //查询buildingName
        Building building = buildingDao.selectById(buildingId);
        //查询区域名称
        Area area = areaDao.selectById(building.getAreaId());
        //拼接楼宇信息
        String buildingName = area.getAreaName() + "-" + building.getBuildingName();
        return buildingName;
    }
}
